package models

import (
	"chinasoccer/pkg/utils"
	"fmt"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// 供应商数据:供应商编号，供应商名称，供应商简称，地址，公司电话，邮件，联系人，联系人电话，备注
type Supplier struct {
	gorm.Model
	SupplierID         string `json:"supplierid" form:"supplierid" gorm:"type:varchar(20);not null;unique;" binding:"required"`
	SupplierName       string `json:"suppliername" form:"suppliername" gorm:"type:varchar(20);not null;" binding:"required"`
	SupplierShortName  string `json:"suppliershortname" form:"suppliershortname" gorm:"type:varchar(20);not null;" binding:"required"`
	SupplierEmail      string `json:"supplieremail" form:"supplieremail" gorm:"type:varchar(20);not null;" binding:"required"`
	SupplierAddress    string `json:"supplieraddress" form:"supplieraddress" gorm:"type:varchar(20);not null;" binding:"required"`
	SupplierPhone      string `json:"supplierphone" form:"workphone" gorm:"type: varchar(20);not null;unique" binding:"required"`
	SupplierOwner      string `json:"supplierowner" form:"supplierowner" gorm:"type:varchar(20);not null;" binding:"required"`
	SupplierOwnerPhone string `json:"supplierownerphone" form:"supplierownerphone" gorm:"type:varchar(20);not null;" binding:"required"`
	SupplierOthers     string `json:"supplierothers" form:"supplierownerothers" gorm:"type:varchar(20);not null;" binding:"required"`
	IsDel              bool   `json:"isdel" form:"isdel" gorm:"type:boolean default false;"`
}

func getJson1(c *gin.Context) Supplier {
	json := make(map[string]string, 1024)
	c.ShouldBind(&json)
	supplierID := json["supplierid"]
	supplierName := json["suppliername"]
	supplierShortName := json["suppliershortname"]
	supplierEmail := json["supplieremail"]
	supplierAddress := json["supplieraddress"]
	supplierPhone := json["supplierphone"]
	supplierOwner := json["supplierowner"]
	supplierOwnerPhone := json["supplierownerphone"]
	supplierOthers := json["supplierothers"]
	supplier := Supplier{
		SupplierID:         supplierID,
		SupplierName:       supplierName,
		SupplierShortName:  supplierShortName,
		SupplierEmail:      supplierEmail,
		SupplierPhone:      supplierPhone,
		SupplierOwner:      supplierOwner,
		SupplierOwnerPhone: supplierOwnerPhone,
		SupplierAddress:    supplierAddress,
		SupplierOthers:     supplierOthers,
	}
	fmt.Println(supplier)
	return supplier
}

// 获取所有的 Suppliers
func GetAllSupplier(supplierParam map[string]interface{}) ([]Supplier, int64, error) {
	var supplierdata []Supplier
	pagenum := supplierParam["pagenum"].(int)
	pagesize := supplierParam["pagesize"].(int)
	query := supplierParam["query"].(string)
	if query == "" {
		var total int64
		err := db.Table("suppliers").Where("is_del = false").Count(&total).Error
		if err != nil {
			return supplierdata, 0, err
		}
		err = db.Table("suppliers").Where("is_del = false").Offset((pagenum - 1) * pagesize).Limit(pagesize).Find(&supplierdata).Error
		fmt.Println("total = ", total)
		return supplierdata, total, err
	}
	supplierName := query

	var supplier Supplier
	err := db.Table("suppliers").Where("supplier_id = ?", supplierName).First(&supplier).Error
	supplierdata = append(supplierdata, supplier)
	if err != nil {
		return supplierdata, 0, err
	}

	return supplierdata, 1, err
}

// 增加员工的信息
func AddSupplier(c *gin.Context) (Supplier, int64, error) {
	supplier := getJson1(c)
	var total int64
	db.Table("suppliers").Where("supplier_id = ? or supplier_phone = ?", supplier.SupplierID, supplier.SupplierPhone).Count(&total)
	if total == 0 {
		err := db.Table("suppliers").Create(&supplier).Error
		return supplier, total, err
	}
	return supplier, total, nil
}

// 修改员工信息
func UpdateSupplier(c *gin.Context) (Supplier, error) {
	supplier := getJson1(c)
	err := db.Table("suppliers").Where("supplier_id = ?", supplier.SupplierID).Updates(&supplier).Error
	return supplier, err
}

// 删除员工
func DelSupplier(c *gin.Context) error {
	id := c.Params.ByName("id")
	err := db.Table("suppliers").Where("supplier_id = ?", id).Update("is_del", true).Error
	if err != nil {
		return err
	}
	err = db.Table("suppliers").Where("supplier_id = ?", id).Update("supplier_id", utils.RandomString(19)).Error
	return err
}
